package com.example.administrator.newjiajiesong.main.activity.mvp.presenter;

import android.text.TextUtils;


import com.example.administrator.newjiajiesong.main.activity.LoginActivity;
import com.example.administrator.newjiajiesong.main.activity.mvp.contract.FindPasswordContract;
import com.example.administrator.newjiajiesong.main.activity.mvp.model.LoginModelImpl;
import com.example.administrator.newjiajiesong.network.ApiCallBack;
import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.observers.DefaultObserver;


/**
 * Created by Administrator on 2017/04/20
 */

public class FindPasswordPresenterImpl extends MvpBasePresenter<FindPasswordContract.View> implements FindPasswordContract.Presenter {

    private LoginModelImpl mModel;

    public FindPasswordPresenterImpl() {
        mModel = new LoginModelImpl();
    }

    @Override
    public void sendCode() {
        if (getView() == null) {
            return;
        }
        String mobile = getView().getMobile();
        //验证
        if (verifyInput(mobile)) {
            getView().showProgressDialog("验证码发送中");
            mModel.sendCode(LoginModelImpl.TYPE_FORGET, mobile, new ApiCallBack() {
                @Override
                protected void onSuccess(Object responseData, String message) {
                    if (getView() != null) {
                        getView().dismissProgressDialog();
                        getView().toast(message);
                    }
                    //发送成功
                    final int count = 60;
                    Observable.interval(0, 1, TimeUnit.SECONDS)
                            .take(count + 1)
                            .map(new Function<Long, Long>() {
                                @Override
                                public Long apply(@NonNull Long aLong) throws Exception {
                                    return count - aLong;
                                }
                            })
                            .observeOn(AndroidSchedulers.mainThread())
                            .doOnSubscribe(new Consumer<Disposable>() {
                                @Override
                                public void accept(@NonNull Disposable disposable) throws Exception {
                                    //设置发送数据时按钮不可用
                                    if (getView() != null) {
                                        getView().setCodeBtnEnable(false);
                                    }
                                }
                            })
                            .subscribe(new DefaultObserver<Long>() {
                                @Override
                                public void onNext(Long aLong) {
                                    if (getView() != null) {
                                        getView().setCodeBtnText("(" + aLong + "s)发送验证码");
                                    }
                                }

                                @Override
                                public void onError(Throwable e) {
                                    if (getView() == null) {
                                        return;
                                    }
                                    getView().setCodeBtnText("发送验证码");
                                    getView().setCodeBtnEnable(true);
                                    getView().toast(e.getMessage());
                                }

                                @Override
                                public void onComplete() {
                                    if (getView() == null) {
                                        return;
                                    }
                                    getView().setCodeBtnText("发送验证码");
                                    getView().setCodeBtnEnable(true);
                                }
                            });
                }

                @Override
                protected void onFailure(String error) {
                    if (getView() != null) {
                        getView().dismissProgressDialog();
                        getView().toast(error);
                    }
                }
            });
        }

    }

    /**
     * 验证手机号
     *
     * @param mobile 手机号
     */
    private boolean verifyInput(String mobile) {
        if (getView() == null) {
            return false;
        }
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            getView().toast("请输入正确的手机号码");
            return false;
        }
        return true;
    }

    /**
     * 验证输入
     *
     * @param mobile       手机号
     * @param password     密码
     * @param surePassword 确认密码
     * @param code         验证码
     */
    private boolean verifyInput(String mobile, String password, String surePassword, String code) {
        if (getView() == null) {
            return false;
        }
        if (TextUtils.isEmpty(mobile) || mobile.length() != 11) {
            getView().toast("请输入正确的手机号码");
            return false;
        }
        if (TextUtils.isEmpty(password)) {
            getView().toast("请输入密码");
            return false;
        }
        if (TextUtils.isEmpty(surePassword)) {
            getView().toast("请确认密码 ");
            return false;
        }
        if (!password.equals(surePassword)) {
            getView().toast("两次密码不一致,请核对");
            return false;
        }
        if (TextUtils.isEmpty(code) || code.length() != 4) {
            getView().toast("请输入正确的验证码");
            return false;
        }
        return true;
    }


    @Override
    public void sureEdit() {
        if (getView() == null) {
            return;
        }
        String mobile = getView().getMobile();
        String password = getView().getPassword();
        String surePassword = getView().getSurePassword();
        String code = getView().getCode();

        if (!verifyInput(mobile, password, surePassword, code)) {
            return;
        }
        //修改密码
        getView().showProgressDialog("");
        mModel.sureEdit(mobile, password, code, new ApiCallBack() {
            @Override
            protected void onSuccess(Object responseData, String message) {
                if (getView() != null) {
                    getView().dismissProgressDialog();
                    //修改成功,去登录
             //       getView().startActivity(LoginActivity.class);
                }
            }

            @Override
            protected void onFailure(String error) {
                if (getView() != null) {
                    getView().dismissProgressDialog();
                    getView().toast(error);
                }
            }
        });
    }
}